﻿using SqlServerMaintenanceSolution.Models.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SqlServerMaintenanceSolution.Models
{
    public class SqlServerAuthentication
    {
        public string DisplayName { get; set; }
        public bool IntegratedSecurity { get; set; }
        public Authentication Authentication { get; set; }
        public string UserID { get; set; }
        public string Password { get; set; }

        public SqlServerAuthentication(Authentication authentication)
        {
            this.Authentication = authentication;
            switch (authentication)
            {
                case Authentication.WindowsAuthentication:
                    this.DisplayName = "Windows Authentication";
                    this.IntegratedSecurity = true;
                    break;
                default:
                    this.DisplayName = "Sql Server Authentication";
                    this.IntegratedSecurity = false;
                    break;
            }
        }

        public SqlServerAuthentication(string authentication)
        {
            this.DisplayName = authentication;
            foreach (Authentication auth in Enum.GetValues(typeof(Authentication)))
            {
                if (auth.ToString() == Regex.Replace(authentication, @"\s+", ""))
                    this.Authentication = auth;

                if(this.Authentication == Enums.Authentication.WindowsAuthentication)
                    this.IntegratedSecurity = true;
                else
                    this.IntegratedSecurity = false;
            }
        }

        public static IEnumerable<SqlServerAuthentication> GetServerAuthentications()
        {
            var serverAuthentications = new List<SqlServerAuthentication>();

            foreach (Authentication auth in Enum.GetValues(typeof(Authentication)))
            {
                serverAuthentications.Add(new SqlServerAuthentication(auth));
            }

            return serverAuthentications;
        }
    }
}
